package testGuava.example.testSpring;

import org.junit.Test;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @ClassName : org.example.testSpring.ReflectUtilsTest
 * @Description : 类描述
 * Created by cyd on 2021-09-15 16:18:29
 */
public class ReflectUtilsTest {

    /**
     * 获取属性
     */
    @Test
    public void test() {
        Person person = new Person();
        person.setAge(10);
        person.setName("mike");
         
        ReflectionUtils.doWithFields(Person.class, new ReflectionUtils.FieldCallback() {
            @Override
            public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
                field.setAccessible(true);
                Object fieldValue = ReflectionUtils.getField(field, person);
                System.out.println(fieldValue);
            }
        });


    }

    /**
     * 获取方法
     */
    @Test
    public void test02() {
        ReflectionUtils.doWithMethods(Person.class, new ReflectionUtils.MethodCallback() {
            @Override
            public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
                System.out.println(method);
            }
        });
    }
}

class Person {
    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public void say() {
        System.out.println("hello");
    }
}
